/*
*  Impp - Instant Messenger
*
*  Copyright (C) 2008  Oliver Schneider
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
#ifndef OSCARSERVERCON_H_
#define OSCARSERVERCON_H_

#include <wx/socket.h>
#include <wx/timer.h>


#include "Flap.h"
#include "CliPackage.h"
#include "PackageConstants.h"
#include "Oscar.h"
#include "ReadStatus.h"
#include "RateManagement.h"
#include "tasks/TaskHandle.h"
#include "OscarServerConTypes.h"
#include "tasks/TaskManager.h"
#include "PackageQueue.h"

namespace Impp {

class Oscar;
class TaskManager;
class PackageQueue;

class OscarServerCon : private wxTimer
{
private:
	Oscar* oscar;
	wxSocketClient* client;
	word seqNumber;	
	dword requestId;	
	ReadStatus readStatus;
	State connectionState;
	RateManagement rates;
	PackageQueue* packageQueue;
	
	enum
	{
	  ID_SOCKET = 200
	};
	
public:
	OscarServerCon(Oscar* oscar);
	virtual ~OscarServerCon();
	
	bool connect(wxString srv, int port);
	void disconnect();
	bool isConnected();
	Flap* readPackage();
	bool sendPackage(Chan chan, CliPackage* pack, const Priority & priority = PRIORITY_NORMAL, const TaskHandle & handleType = TASK_HANDLE_NOHANDLE, const qword & handle = 0);
	dword nextRequestId();
	State getConnectionState();
	void onSocketEvent(wxSocketEvent & event);
	void setRates(std::vector<Rate> & rates, std::vector<RateGroup> & rateGroups);
	void setTaskManager(TaskManager* tasks);
	
private:
	bool read(void* data,size_t lng);
	word nextSeqNumber();
	bool doSendPackage(Chan chan, CliPackage* pack);
	void Notify();
	
private:
    DECLARE_EVENT_TABLE()
};

}  // namespace Impp

#endif /*OSCARSERVERCON_H_*/
